package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IPermissionService;
import cn.wolfcode.rbac.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private IPermissionService service;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") QueryObject qo, Model model){
        model.addAttribute("result",this.service.query(qo));
        return "permission/list";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
        this.service.delete(id);
        return new JsonResult("操作成功",true);
    }

    //对LOAD()请求进行异常处理
    @RequestMapping({"/load"})
    @ResponseBody
    public JsonResult load(){
        JsonResult json = new JsonResult();
        try {
            this.service.load();
            return json;
        }catch (Exception e){
            e.printStackTrace();
            json.mark("操作失败");
        }
        return json;
    }
}
